/**!
 *
 * 修改密码。
 *
 */

$(function () {
    /** 修改密码表单的 jQuery 对象 */
    var $passwdForm = $("#passwd_form");
    /** 显示校验信息的容器 */
    var $validatecontainer = $("#validate_container");
    /** 修改按钮的 jQuery 对象 */
    var $updatePasswdBtn = $passwdForm.find("button[name='updatePasswdBtn']");

    $.validator.addMethod("checkOldPasswd", function (value, element) {
        return this.optional(element) || passwdRE.test(value);
    });

    $.validator.addMethod("checkNewPasswd", function (value, element) {
        return this.optional(element) || passwdRE.test(value);
    });

    $passwdForm.validate({
        rules: {
            oldPasswd: {
                required: true,
                minlength: 6,
                maxlength: 20,
                checkOldPasswd: true
            },
            newPasswd: {
                required: true,
                minlength: 6,
                maxlength: 20,
                checkNewPasswd: true
            },
            confirmNewPasswd: {
                required: true,
                minlength: 6,
                maxlength: 20,
                equalTo: "#new_passwd"
            }
        },
        messages: {
            oldPasswd: {
                required: "<span class='font-size-12'><i class='fa fa-times-circle'></i>请输入旧密码</span>",
                minlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>旧密码的长度不能小于{0}个字</span>",
                maxlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>旧密码的长度不能大于{0}个字</span>",
                checkOldPasswd: "<span class='font-size-12'><i class='fa fa-times-circle'></i>旧密码由数字、字母组成，且长度在6至20个字符之间</span>",
            },
            newPasswd: {
                required: "<span class='font-size-12'><i class='fa fa-times-circle'></i>请输入新密码</span>",
                minlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>新密码的长度不能小于{0}个字</span>",
                maxlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>新密码的长度不能大于{0}个字</span>",
                checkNewPasswd: "<span class='font-size-12'><i class='fa fa-times-circle'></i>新密码由数字、字母组成，且长度在6至20个字符之间</span>",
            },
            confirmNewPasswd: {
                required: "<span class='font-size-12'><i class='fa fa-times-circle'></i>请输入确认新密码</span>",
                minlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>确认新密码的长度不能小于{0}个字</span>",
                maxlength: "<span class='font-size-12'><i class='fa fa-times-circle'></i>确认新密码的长度不能大于{0}个字</span>",
                equalTo: "<span class='font-size-12'><i class='fa fa-times-circle'></i>确认新密码与新密码不一致</span>",
            }
        },
        errorContainer: $validatecontainer,
        errorLabelContainer: $("ul", $validatecontainer),
        wrapper: "li",
        submitHandler: function (form) {
            $updatePasswdBtn.html("<i class='ace-icon fa fa-spinner fa-spin bigger-150'></i>正在修改…").attr({"disabled": "disabled"});

            return true;
        }
    });
});